<template>
  <div class="permissions-container">
    <div class="app-container">
      <el-card>
        <el-button
          type="primary"
          size="small"
          @click="handleOpenAddDialog(1, '0')"
        >添加权限</el-button>
        <el-table
          border
          stripe
          style="margin-top:20px"
          :data="permTreeData"
          row-key="id"
          default-expand-all
        >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                type="text"
                @click="handleOpenAddDialog(2, row.id)"
              >添加权限点</el-button>
              <el-button type="text" @click="handleOpenEditDailog(row.id)">编辑</el-button>
              <el-button type="text" @click="handleDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        title="新增权限(点)"
        :visible="showAddDialog"
        @close="handleAddClose"
      >
        <el-form label-width="80px">
          <el-form-item label="权限名称">
            <el-input v-model="permForm.name" style="width:95%" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="permForm.code" style="width:95%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="permForm.description" style="width:95%" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permForm.enVisible"
              active-color="#409EFF"
              inactive-color="#ccc"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleAddClose">取消</el-button>
          <el-button
            type="primary"
            @click="handleAdd"
          >确定</el-button>
        </template>
      </el-dialog>
      <!-- 编辑弹框 -->
      <el-dialog
        title="编辑权限(点)"
        :visible="showEditDialog"
        @close="handleEditClose"
      >
        <el-form label-width="80px">
          <el-form-item label="权限名称">
            <el-input v-model="permForm.name" style="width:95%" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="permForm.code" style="width:95%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="permForm.description" style="width:95%" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permForm.enVisible"
              active-color="#409EFF"
              inactive-color="#ccc"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleEditClose">取消</el-button>
          <el-button
            type="primary"
            @click="handleEdit"
          >确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getPermListAPI,
  addPermAPI,
  getPermDetailAPI,
  editPermAPI,
  delPermAPI
} from '@/api/permissions'
import { arr2Tree } from '@/utils/shift'
export default {
  name: 'PermissionsView',
  data() {
    return {
      // 权限树形数据
      permTreeData: [],
      // 控制新增权限弹框的显示
      showAddDialog: false,
      // 控制编辑权限弹框的显示
      showEditDialog: false,
      // 权限表单
      permForm: {
        name: '',
        code: '',
        description: '',
        enVisible: '1'
      }
    }
  },
  created() {
    this.getPermList()
  },
  methods: {
    // 删除权限
    async handleDel(id) {
      try {
        await delPermAPI(id)
        this.getPermList()
      } catch (err) {
        this.$message.error(err.message || '操作失败')
      }
    },
    // 编辑权限
    async handleEdit() {
      // 注意：建议在调用所有api接口函数的时候，都主动捕捉错误
      // 用try-catch语句块捕获，这样可以保证代码的健壮性
      try {
        // 校验
        // 发起编辑请求
        await editPermAPI(this.permForm)
        // 关闭弹框
        this.handleEditClose()
        // 获取权限列表
        this.getPermList()
      } catch (err) {
        this.$message.error(err.message || '操作失败')
      }
    },
    // 编辑权限弹框打开
    async handleOpenEditDailog(id) {
      // 显示弹框
      this.showEditDialog = true
      // 发起获取详情的请求
      const resp = await getPermDetailAPI(id)
      // 赋值(做回显)
      this.permForm = resp.data
    },
    // 编辑权限弹框关闭
    handleEditClose() {
      this.showEditDialog = false
    },
    // 新增权限弹框打开时
    handleOpenAddDialog(type, pid) {
      // type: 1 -> 页面访问权，2 -> 按钮操作权
      // pid: 父级id, '0'表示页面访问权，否则就是按钮操作权
      // 保存type和pid，给handleAdd用
      this.type = type
      this.pid = pid
      // 显示弹框
      this.showAddDialog = true
    },
    // 新增权限
    async handleAdd() {
      // 校验
      // 发起新增请求
      await addPermAPI({
        ...this.permForm,
        type: this.type,
        pid: this.pid
      })
      // 关闭弹框
      this.handleAddClose()
      // 获取权限列表
      this.getPermList()
    },
    // 新增权限弹框关闭
    handleAddClose() {
      this.showAddDialog = false
    },
    // 获取权限列表
    async getPermList() {
      // 发起获取列表请求
      const resp = await getPermListAPI()
      // 把列表转为树形
      this.permTreeData = arr2Tree(resp.data, '0')
    }
  }
}
</script>

<style>

</style>
